<?php
//模板解析引擎
//实现了部分解析功能
class view{
	const TPL = 'view';        //模板放置文件件
	private $temp;             //存放需要解析及替换的变量及值
	static private $instance;
	
	public function __construct(){
		$this->getInstance();
	}
	
	protected function &getInstance(){
		if (self::$instance == null) {
			self::$instance = new view();
		}
		return self::$instance;
	}
	
	public function assign($tplvar,$value){
		if (!empty($tplvar) && !empty($value)) {
			$this->temp[$tplvar] = $value;
		}else {
			exit('missing values!');
		}
	}
	
	protected function parse($tplname){
		$fp = fopen($tplname);
		$content = file_get_contents($tplname);
		foreach ($this->temp as $vName => $value){
			$content = str_ireplace('{'.$vName.'}',$value,$content);
		}
		if(file_put_contents($tplname,$content)){
			fclose($fp);
		}
	}
	
	public function display($tplname){
		$filename = ROOT.TPL.$tplname.EXT;
		if (is_file($filename) && is_writable($filename)) {
			$this->parse($filename);
		}
	}
}